package com.lovo.csc.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lovo.csc.entity.Settlement;
import com.lovo.csc.entity.Vendororder;
import com.lovo.csc.entity.Vendororderinfo;
import com.lovo.csc.service.ISettlementService;
import com.lovo.csc.service.IVendororderService;
import com.lovo.csc.service.IVendororderinfoService;
import com.lovo.psc.service.IPscUserService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/vendororder")
@Api(tags = "供货订单控制层--曾亚军--002")
public class VendororderController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    IVendororderService vendororderService;
    @Autowired
    IVendororderinfoService vendororderinfoService;
    @Autowired
    ISettlementService settlementService;
    @Autowired
    IPscUserService pscUserService;
    @ApiOperation("查询所有供货订单")
    @GetMapping("findByItem")
    public List<Vendororder> findByItem(String state,String vendorName){
        return vendororderService.findByItem(state, vendorName);
    }
    @ApiOperation("修改供货订单状态")
    @PutMapping("updateOrderState/{vendorOrderCode}/{state}/{updateName}")
    public String updateOrderState(@PathVariable("vendorOrderCode") String vendorOrderCode,
                                   @PathVariable("state") String state,
                                   @PathVariable("updateName") String updateName){
        //修改订单状态
        UpdateWrapper<Vendororder> uw = new UpdateWrapper<>();
        uw.eq("vendorCode",vendorOrderCode);
        uw.set("vendorOrderState",state);
        vendororderService.update(uw);
        //保存修改记录
        Vendororderinfo vendororderinfo = new Vendororderinfo();
        vendororderinfo.setOrderchangepeople(updateName);
        vendororderinfo.setOrderchange(state);
        vendororderinfo.setVendorordercode(vendorOrderCode);
        vendororderinfoService.save(vendororderinfo);
        return "ok";
    }
    @ApiOperation("订单结算")
    @PutMapping("payment")
    public String payment(Integer orderId,String state){
        //调用付款接口付款
        //改变本地供货订单状态
        Vendororder order =  vendororderService.getById(orderId);
        order.setVendororderstate(state);
        vendororderService.updateById(order);
        //得到供货订单编号
        String code = order.getVendorcode();
        //改变供应商供货订单状态
        pscUserService.updateState(code,state);
        //保存订单修改记录
        Vendororderinfo vendororderinfo = new Vendororderinfo();
        vendororderinfo.setOrderchange(state);
        vendororderinfo.setVendorordercode(code);
        vendororderinfo.setOrderchangepeople("zy");
        vendororderinfoService.save(vendororderinfo);
        //生成结算单
        String total = order.getVendorordertotal();
        Settlement settlement = new Settlement();
        settlement.setSettlementtotal(total);
        settlement.setVendororderid(code);
        //保存结算单
        settlementService.save(settlement);
        //投递结算单
        rabbitTemplate.convertAndSend(MQUtil.directExchangeSettlement,MQUtil.settlementKey,settlement);
        return "ok";
   }
}
